
// Promise then
function runAsync() {
    return new Promise(function (resolve, reject) {
        //做一些异步操作
        setTimeout(function () {
            console.log('执行完成');
            resolve('返回数据');
        }, 2000);
    });
}

runAsync().then(function (data) {
    console.log(data);
});



// Promise 链式调用、成功与失败、catch、finally
function runAsync1() {
    return new Promise(function (resolve, reject) {
        //做一些异步操作
        setTimeout(function () {
            console.log('执行成功');
            resolve('success');
        }, 2000);
    });
}

function runAsync2() {
    return new Promise(function (resolve, reject) {
        //做一些异步操作
        setTimeout(function () {
            console.log('执行失败');
            reject('failed');
        }, 2000);
    });
}

runAsync1()
    .then(function (data) {
        console.log(data);
        return runAsync1();
    })
    .then(function (data) {
        console.log(data);
        return runAsync1();
    })
    .then(function (data) {
        console.log(data);
        return runAsync2();
    })
    .then(function (data) {
        console.log(data);
        return runAsync1();
    })
    .then(function (data) {
        console.log(data);
    })
    .catch(function (data) {
        console.log("异常：" + data);
    })
    .finally(function () {
        console.log("结束");
    })
;